package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.*;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.modular.system.dao.CollectLikeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 用户收藏控制器
 *
 * @author fengshuonan
 * @Date 2018-10-18 09:37:35
 */
@Controller
@RequestMapping("/collectLike")
public class CollectLikeController extends BaseController {

    private String PREFIX = "/system/collectLike/";

    @Autowired
    CollectLikeMapper collectLikeMapper;
    @Autowired
    CheckAccessUtil checkAccessUtil;
    @Autowired
    CollectLikeDao collectLikeDao;
    @Autowired
    OrgInfoMapper orgInfoMapper;
    @Autowired
    OrgTeacherMapper orgTeacherMapper;
    @Autowired
    OrgClassTypeMapper orgClassTypeMapper;
    @Autowired
    OrgUserMapper orgUserMapper;


    @RequestMapping("")
    public String index() {
        return PREFIX + "collectLike.html";
    }

    /**
     * 跳转到添加用户收藏
     */
    @RequestMapping("/collectLike_add")
    public String collectLikeAdd() {
        return PREFIX + "collectLike_add.html";
    }

    /**
     * 跳转到修改用户收藏
     */
    @RequestMapping("/collectLike_update/{collectLikeId}")
    public String collectLikeUpdate(@PathVariable Integer collectLikeId, Model model) {
        return PREFIX + "collectLike_edit.html";
    }

    /**
     * 获取用户收藏列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<CollectLike> collectLikes = null;
        int infoId =  checkAccessUtil.CheckAccess();
        if(infoId == 0){
            collectLikes = collectLikeMapper.selectList(null);
        }else if(infoId == -1){
            return null;
        }else{
            collectLikes = collectLikeDao.findByInfoId(infoId);
        }
        for(CollectLike collectLike:collectLikes){
            String infoIds = collectLike.getInfoId();
            String userIds = collectLike.getUserId();
            OrgUser opUser = orgUserMapper.selectById(userIds);
            OrgInfo orgInfo = orgInfoMapper.selectById(infoIds);
            String infoName = orgInfo.getOrgName();
            String userName = opUser.getUserName();
            collectLike.setInfoId(infoName);
            collectLike.setUserId(userName);
        }

        return collectLikes;
    }

    /**
     * 新增用户收藏
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除收藏
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除收藏",key = "collectLikeId",dict = Dict.DeleteDict)
    public Object delete(@RequestParam long collectLikeId) {
        collectLikeMapper.deleteById(collectLikeId);
        return SUCCESS_TIP;
    }


    /**
     * 修改用户收藏
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 用户收藏详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
